<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 订单管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <template>
                <el-tabs v-model="activeName" @tab-click="handleClick">
                    <el-tab-pane label="全部订单" name= ""></el-tab-pane>
                    <el-tab-pane label="待付款"   name="1"></el-tab-pane>
                    <el-tab-pane label="待发货"   name="2"></el-tab-pane>
                    <el-tab-pane label="待收货"   name="3"></el-tab-pane>
                    <el-tab-pane label="已收货"   name="4"></el-tab-pane>
                </el-tabs>
                </template>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                
                <el-table-column prop="oid" label="订单号" width="70" align="center"></el-table-column>
                <el-table-column prop="name" label="收货人"></el-table-column>
                <el-table-column prop="addr" label="收货地址"></el-table-column>
                <el-table-column prop="phone" label="联系方式"></el-table-column>
                <el-table-column prop="ordertime" label="创建时间"></el-table-column>
                
              

               
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >订单详情</el-button>

                        <el-button v-if="scope.row.state==2"
                            type="text"
                            class="el-icon-lx-cartfill"
                            @click="ConfirmDelivery(scope.row.oid)"
                        >确认发货</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageNow"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" @click="getProductByOid" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="订单号">
                    <el-input :disabled="true" v-model="form.oid"></el-input>
                </el-form-item>
                <el-form-item label="收货人">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="收货地址">
                    <el-input v-model="form.addr"></el-input>
                </el-form-item>
                <el-form-item label="联系电话">
                    <el-input v-model="form.phone"></el-input>
                </el-form-item>
            </el-form>
            <el-table :data="product">
                <el-table-column prop="pid" label="商品编号" width="70" align="center"></el-table-column>
                <el-table-column prop="product.pname" label="商品名"></el-table-column>
                <el-table-column label="商品图片" align="center">
                    <template slot-scope="scope">
                        <el-image
                            class="table-td-thumb"
                            :src="getImg(scope.row.product.image)"
                            :preview-src-list="[getImg(scope.row.product.image)]"
                        ></el-image>
                    </template>
                </el-table-column>
                <el-table-column prop="count" label="数量"></el-table-column>
                <el-table-column prop="subtotal" label="小计"></el-table-column>
            </el-table>
             
            <span slot="footer" class="dialog-footer">
                 <font style="color: red;">总计:{{mytotal}}</font>
                <el-button @click="myRefresh">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import axios from "axios"
import api from "../../api/index"
export default {
    name: 'basetable',
    data() {
        return {
            activeName: 'second',
            query: {
                state:"",
                
                pageNow: 1,
                pageSize: 10
            },
            myOrders:{},
            mytotal:0,
            product:[],
            tableData: [],
            multipleSelection: [],
            delList: [],
            order:{
                
            },
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1
        };
    },
    created() {
     this.getOrders()
    },
    methods: {
        getImg(src){
            return "/api/"+src
        },
        //获取订单
        getOrders(){
            api.orderApi.getOrders(this.query)
            .then(res=>{
               this.query.pageNow=res.data.pageNum
               this.query.pageSize=res.data.pageSize
               this.tableData=res.data.list
                this.pageTotal=res.data.total
                
            })
            .catch(err=>{
               console.log(err)
           })   
        },
        //确认发货
        ConfirmDelivery(oid){
            api.orderApi.ConfirmDelivery(oid)
            .then(res=>{
               this.getOrders()
            })
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageNow', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
           this.ConfirmDelivery(row.oid)
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        //根据oid获取订单详情
        getProductByOid(oid){
            api.orderApi.getProductByOid(oid)
            .then(res=>{
               this.mytotal=0
               this.product=res.data
               for(var i=0;i<res.data.length;i++){
                   this.mytotal+=res.data[i].subtotal
               }
               console.log(res.data)
            })
            .catch(err=>{
               console.log(err)
           })
        },
        //更改订单详情信息
        updateOrders(){
            api.orderApi.updateOrders(this.form)
             .then(res=>{
               console.log(res)
            })
            .catch(err=>{
               console.log(err)
           })
        },
        // 编辑操作
        handleEdit(index, row) {
            this.getProductByOid(row.oid)
            this.idx = index;
            this.form = row;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            this.updateOrders()
            
            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            this.$set(this.tableData, this.idx, this.form);
            
        },
        //取消编辑
        myRefresh(){
             this.editVisible = false;
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageNow', val);
            this.getOrders()
        },
        handleClick(tab, event) {
            this.query.state=tab.name
            this.$set(this.query, 'pageNow', 1);
            this.getOrders()  
            console.log(tab, event);
      }
      
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
